package com.platform.controller.backend;

import com.github.pagehelper.PageInfo;
import com.platform.base.BaseController;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.pojo.Menu;
import com.platform.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 菜单控制层
 */
@RestController
@RequestMapping("/menuController/")
@CrossOrigin(origins = Const.REQUESTURL,maxAge = 3600,allowCredentials = "true")
public class MenuController extends BaseController {



    @PostMapping(value = "add.do")
    public ServerResponse<String> insert(Menu menu){
        return iMenuService.insert(menu);
    }

    @PostMapping(value = "del.do")
    public ServerResponse<String> del(Integer id){
        return iMenuService.del(id);
    }

    @PostMapping(value = "update.do")
    public ServerResponse<String> update(Menu menu){
        return iMenuService.update(menu);
    }

    /**
     * 根据用户角色查询菜单
     * @param request
     * @param parentId
     * @return
     */
    @GetMapping(value = "selectByUserName.do")
    public ServerResponse<List<Map>> selectByUserName(HttpServletRequest request, Integer parentId){
        Object object = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == object){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        User user = (User)object;
        return iMenuService.selectByUserName(user.getUsername(),parentId);
    }

    /**
     * 查询特定格式菜单
     * @return
     */
    @GetMapping(value = "selectAllMenu.do")
    public ServerResponse<List<Map>> selectAllMenu(@RequestParam(value = "roleCode") String roleCode,
                                                   HttpServletRequest request){
        Object object = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == object){
            return ServerResponse.createByErrorMessage("请先登录");
        }

        return iMenuService.selectAllMenu(roleCode,0);
    }


    /**
     * 根据条件查询菜单
     * @param param
     * @return
     */
    @GetMapping(value = "selectByParam.do")
    public ServerResponse<PageInfo> selectByParam(@RequestParam Map param){
        return iMenuService.selectByParam(param);
    }

    /**
     * 查询所有一级菜单
     * @return
     */
    @GetMapping(value = "selectAllFirstMenu.do")
    public ServerResponse<List<Map>> selectAllFirstMenu(){
        return iMenuService.selectAllFirstMenu();
    }

}
